package service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IIfwDeviceAuzInfo;
import service.interf.IOperationLog;

import common.impl.exception.ServiceException;

import dao.interf.IIfwDeviceAuzInfoDAO;
import dao.interf.IIfwSrouterMapDAO;
import dao.pojo.IfwDeviceAuzInfo;
import dao.pojo.IfwSrouterMap;

public class IfwDeviceAuzInfoImpl extends BaseDataServiceImpl implements
		IIfwDeviceAuzInfo {

	private IIfwDeviceAuzInfoDAO ifwDeviceAuzInfoDAO;
	private IIfwSrouterMapDAO ifwSrouterMapDao;
	private IOperationLog iOperationLog;

	public void setIfwDeviceAuzInfoDAO(IIfwDeviceAuzInfoDAO ifwDeviceAuzInfoDAO) {
		this.ifwDeviceAuzInfoDAO = ifwDeviceAuzInfoDAO;
	}

	public void setIfwSrouterMapDao(IIfwSrouterMapDAO ifwSrouterMapDao) {
		this.ifwSrouterMapDao = ifwSrouterMapDao;
	}

	public void setiOperationLog(IOperationLog iOperationLog) {
		this.iOperationLog = iOperationLog;
	}

	/**
	 * 获取主机数
	 * 
	 * @param sn
	 * @return
	 * @throws Exception
	 */
	public String getHostAllowed(String sn) throws Exception {
		List<IfwDeviceAuzInfo> list = ifwDeviceAuzInfoDAO.findByProperties(
				new String[] { "sn" }, new Object[] { sn });
		int host_allowed = 200;
		if (null != list && !list.isEmpty()) {
			Integer ha = list.get(0).getHost_allowed();
			if (null != ha && ha.intValue() > 0)
				host_allowed = ha.intValue();
		}
		return String.valueOf(host_allowed);
	}

	public String add(String model, String sn, int auzTime, int giveTime)
			throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(model))
			throw new ServiceException("设备型号不能为空");
		if (StringUtils.isEmpty(sn))
			throw new ServiceException("设备SN不能为空");
		long day_add = 0;
		if (1 == auzTime) {
			day_add = 30;
		} else if (3 == auzTime) {
			day_add = 90;
		} else if (6 == auzTime) {
			day_add = 180;
		} else if (12 == auzTime) {
			day_add = 365;
		} else if (24 == auzTime) {
			day_add = 365 * 2;
		} else if (36 == auzTime) {
			day_add = 365 * 3;
		} else if (99 == auzTime) {
			day_add = 0;
		} else {
			throw new ServiceException("授权时间参数错误");
		}
		if (giveTime < 0 || giveTime > 30)
			throw new ServiceException("赠送时间必须为0-30天");
		List<IfwSrouterMap> list = ifwSrouterMapDao.findByProperties(
				new String[] { "fwsn" }, new Object[] { sn });
		if (null != list && !list.isEmpty())
			sn = list.get(0).getSn();
		long total = ifwDeviceAuzInfoDAO.getPropertiesCount(
				new String[] { "sn" }, new Object[] { sn }, null, null);
		if (total > 0)
			throw new ServiceException("已存在该设备记录，授权失败！");
		IfwDeviceAuzInfo auzInfo = new IfwDeviceAuzInfo();
		auzInfo.setModel(model);
		auzInfo.setSn(sn);
		if (day_add == 0) {
			auzInfo.setExpireTime("2037-12-30 12:00:00");
		} else {
			Date date = new Date();
			long time = date.getTime();
			if (giveTime > 0)
				day_add += giveTime;
			time += day_add * 24 * 60 * 60 * 1000;
			date.setTime(time);
			String date8 = myDateHandle.MyDateToString(date);
			String expireTime = date8.substring(0, 4) + "-"
					+ date8.substring(4, 6) + "-" + date8.substring(6, 8)
					+ " 12:00:00";
			auzInfo.setExpireTime(expireTime);
		}
		auzInfo.setAcMax(5);
		auzInfo.setAc_allowed(5);
		auzInfo.setActiveLast("2018-01-01 00:00:00");
		auzInfo.setCpu(0);
		auzInfo.setHostMax(100);
		auzInfo.setHost_allowed(150);
		auzInfo.setIp("NULL");
		auzInfo.setMem(0);
		auzInfo.setOnline(0);
		auzInfo.setPort(80);
		auzInfo.setRx(0);
		auzInfo.setTx(0);
		auzInfo.setSysVer("");
		auzInfo.setTimeLeft(0L);
		auzInfo.setWanIp1("0.0.0.0");
		auzInfo.setWanIp2("0.0.0.0");
		auzInfo.setWanIp3("0.0.0.0");
		ifwDeviceAuzInfoDAO.save(auzInfo);
		iOperationLog.add(2, sn, null, String.valueOf(auzTime), String
				.valueOf(giveTime), model, null);
		return "1";
	}

	public String change(String sn, String sn2) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(sn))
			throw new ServiceException("设备SN不能为空");
		if (StringUtils.isEmpty(sn2))
			throw new ServiceException("另一设备SN不能为空");
		List<IfwSrouterMap> list = ifwSrouterMapDao.findByProperties(
				new String[] { "fwsn" }, new Object[] { sn });
		String fwsn = sn;
		if (null != list && !list.isEmpty())
			sn = list.get(0).getSn();
		list = ifwSrouterMapDao.findByProperties(new String[] { "fwsn" },
				new Object[] { sn2 });
		String fwsn2 = sn2;
		if (null != list && !list.isEmpty())
			sn2 = list.get(0).getSn();
		// 验证sn
		List<IfwDeviceAuzInfo> auzInfos = ifwDeviceAuzInfoDAO.findByProperties(
				new String[] { "sn" }, new Object[] { sn });
		if (null == auzInfos || auzInfos.isEmpty())
			throw new ServiceException("设备" + fwsn + "未找到");
		IfwDeviceAuzInfo auzInfo = auzInfos.get(0);
		String expireTime = auzInfo.getExpireTime();
		if (StringUtils.isEmpty(expireTime))
			throw new ServiceException("未查询到" + fwsn + "的到期时间");
		// 验证sn2
		List<IfwDeviceAuzInfo> auzInfos2 = ifwDeviceAuzInfoDAO
				.findByProperties(new String[] { "sn" }, new Object[] { sn2 });
		if (null == auzInfos2 || auzInfos2.isEmpty())
			throw new ServiceException("设备" + fwsn2 + "未找到");
		IfwDeviceAuzInfo auzInfo2 = auzInfos2.get(0);
		String expireTime2 = auzInfo2.getExpireTime();
		if (StringUtils.isEmpty(expireTime2))
			throw new ServiceException("未查询到" + fwsn2 + "的到期时间");
		// 转移
		auzInfo.setExpireTime(expireTime2);
		ifwDeviceAuzInfoDAO.update(auzInfo);
		auzInfo2.setExpireTime(expireTime);
		ifwDeviceAuzInfoDAO.update(auzInfo2);
		iOperationLog.add(3, sn2, sn, expireTime, expireTime2, null, null);
		return "1";
	}

	public String getExpireTime(String sn) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(sn))
			throw new ServiceException("设备SN不能为空");
		List<IfwSrouterMap> list = ifwSrouterMapDao.findByProperties(
				new String[] { "fwsn" }, new Object[] { sn });
		String fwsn = sn;
		if (null != list && !list.isEmpty())
			sn = list.get(0).getSn();
		List<IfwDeviceAuzInfo> auzInfos = ifwDeviceAuzInfoDAO.findByProperties(
				new String[] { "sn" }, new Object[] { sn });
		if (null == auzInfos || auzInfos.isEmpty())
			throw new ServiceException("设备" + fwsn + "未找到");
		IfwDeviceAuzInfo auzInfo = auzInfos.get(0);
		String expireTime = auzInfo.getExpireTime();
		if (StringUtils.isEmpty(expireTime))
			throw new ServiceException("未查询到" + fwsn + "的到期时间");
		return expireTime;
	}

	public String setExpireTime(String sn, String expireTime) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(sn))
			throw new ServiceException("设备SN不能为空");
		if (StringUtils.isEmpty(expireTime))
			throw new ServiceException("到期时间不能为空");
		if (!expireTime.endsWith("12:00:00"))
			throw new ServiceException("到期时间格式错误");
		String temp = myDateHandle.MyISODateTimeStringToString(expireTime);
		if (null == temp)
			throw new ServiceException("到期时间格式错误");
		List<IfwSrouterMap> list = ifwSrouterMapDao.findByProperties(
				new String[] { "fwsn" }, new Object[] { sn });
		if (null != list && !list.isEmpty())
			sn = list.get(0).getSn();
		List<IfwDeviceAuzInfo> auzInfos = ifwDeviceAuzInfoDAO.findByProperties(
				new String[] { "sn" }, new Object[] { sn });
		String expireTimeOld = "";
		if (null == auzInfos || auzInfos.isEmpty()) {
			IfwDeviceAuzInfo auzInfo = new IfwDeviceAuzInfo();
			auzInfo.setModel("fwrouter");
			auzInfo.setSn(sn);
			auzInfo.setExpireTime(expireTime);
			auzInfo.setAcMax(5);
			auzInfo.setAc_allowed(5);
			auzInfo.setActiveLast("2018-01-01 00:00:00");
			auzInfo.setCpu(0);
			auzInfo.setHostMax(100);
			auzInfo.setHost_allowed(150);
			auzInfo.setIp("NULL");
			auzInfo.setMem(0);
			auzInfo.setOnline(0);
			auzInfo.setPort(80);
			auzInfo.setRx(0);
			auzInfo.setTx(0);
			auzInfo.setSysVer("");
			auzInfo.setTimeLeft(0L);
			auzInfo.setWanIp1("0.0.0.0");
			auzInfo.setWanIp2("0.0.0.0");
			auzInfo.setWanIp3("0.0.0.0");
			ifwDeviceAuzInfoDAO.save(auzInfo);
		} else {
			IfwDeviceAuzInfo auzInfo = auzInfos.get(0);
			expireTimeOld = auzInfo.getExpireTime();
			auzInfo.setExpireTime(expireTime);
			ifwDeviceAuzInfoDAO.update(auzInfo);
		}
		iOperationLog.add(4, sn, null, expireTime, expireTimeOld, null, null);
		return "1";
	}
}
